$(function(){
		var last_order = 0;
		
		$(".search_table").hide();
		$(".salary_update_cal").hide();
		
		$(".search_btn").click(function(){
			$(".search_table").hide();
			$(".search_table").fadeIn(500);
			serarch_items(0,10);
		});
		
		var serarch_items = function(offset,pageSize){
			var item_type = $(".search_salary_item_type option:selected").val();
			var item_name = $(".search_salary_item_name").val();
			$.ajax({
				type : "POST",
				url : "salaryItem/searchByPage",
				data:{
					"itemName":item_name,
					"itemType":item_type,
					"offset":offset,
					"pageSize":pageSize
				},
				success : function(data) {
					if(data.error){
						$(".alert_modal").modal('toggle');
						$(".alert_info").text(response.message);
						return;
					}
					ajax_load_table(data);
				}
			});
		}
		var load_table_num = 0;
		var open_delete_modal = 0;
		var page_num = 1;
		var ajax_load_table = function(data){
			load_table_num++;
			var html;
			var page = data.response;
			var jsonData = page.datas;
			$.each(jsonData,function(i){
				var salary=jsonData[i];
				html += 
					"<tr> " +
						"<td><a href='#'>"+salary.salaryItemName+"</a></td> " +
						"<td>"+salary.salaryItemType+"</td>" +
						"<td class='hidden-480'>"+salary.show+"</td>";
					if(salary.salaryCal)
						html += "<td>"+salary.salaryCal+"</td>";
					else
						html += "<td>无</td>";
					html+=
						"<td>"+salary.salaryOrder+"</td>" +
						"<td>"+salary.salaryAddItem+"</td> " +
						"<td>" +
							"<div class='hidden-sm hidden-xs btn-group'>" +
									"<button class='btn btn-xs btn-info salary_item_update_btn' item_id='"+salary.salaryItemId+"'>" +
											"<i class='ace-icon fa fa-pencil bigger-120'></i>" +
									"</button>" +
									"<button class='btn btn-xs btn-danger salary_item_delete_btn' item_id='"+salary.salaryItemId+"'>" +
											"<i class='ace-icon fa fa-trash-o bigger-120'></i>" +
									"</button>";
					if(salary.salaryItemType=="计算"){
							html+=		"<button class='btn btn-xs btn-warning salary_item_update_cal_btn' item_id='"+salary.salaryItemId+"'> " +
												"<i class='ace-icon fa fa-flag bigger-120'></i>" +
										"</button>";
					}
					html+=	"</div>" +
						"</td>" +
					"</tr> ";
			});
			$(".search_container").html("");
			$(".search_container").html(html);
			//分页
			$(".page_num_label").text(page.total+"条");
			
			$(".pagination").html("");
			$(".pagination").append("<li class='paginate_button previous' aria-controls='dynamic-table'><a>Previous</a></li>");
			for(var i=1;i<=page.pageNum;i++){
				var page_btn = "<li class='paginate_button active'' aria-controls='dynamic-table' tabindex='0' page='"+i+"'><a>"+i+"</a></li>";
				$(".pagination").append(page_btn);
			}
			$(".pagination").append("<li class='paginate_button next' aria-controls='dynamic-table'><a>Next</a></li>");

//			if(load_table_num == 1){
				//handle fenye event.
				$(".previous").on("click",function(){
					if(page_num == 1){
						$(this).attr("disabled","disabled");
						return;
					}
					page_num--;
					var offset = (page_num-1)*10;
					serarch_items(offset,10);
				});
				$(".active").on("click",function(){
					var page = $(this).attr("page");
					if(page_num == page)
						return;
					var offset = (page-1)*10;
					serarch_items(offset,10);
					page_num = page;
				});
				$(".next").on("click",function(){
					if(page_num == page.pageNum){
						$(this).attr("disabled","disabled");
						return;
					}
					page_num++;
					var offset = (page_num-1)*10;
					serarch_items(offset,10);
				});
				
				//handle delete event
				$(".salary_item_delete_btn").on("click",function() {
					open_delete_modal++;
					$('.salary_delete_modal').modal('toggle');
					var item_id = $(this).attr("item_id");
					if(open_delete_modal == 1){
						$(".item_delete_submit_btn").on("click",function(){
							console.log("delete : "+item_id);
							$.ajax({
								type : "POST",
								url : "salaryItem/delete",
								data:{
									"itemId":item_id,
								},
								success : function(data) {
									if(data.statusCode == 200)
										serarch_items((page_num-1)*10,10);
									$(".alert_modal").modal('toggle');
									$(".alert_info").text(data.message);
								}
							});
						});
					}
				});
				
				//handle update event
				var open_update_modal = 0;	
				$(".salary_item_update_btn").on("click",function() {
					open_update_modal++; 
					$('.salary_update_modal').modal('toggle');
					var item_id = $(this).attr("item_id");
					//load the info
					$.ajax({
						type : "POST",
						url : "salaryItem/loadItem",
						data:{
							"itemId":item_id
						},
						success : function(data) {
							if(data.error){
								$(".alert_modal").modal('toggle');
								$(".alert_info").text(data.message);
								return;
							}
							console.log(data);
							var salary = data.response;
							$(".salary_update_name").val(salary.salaryItemName);
							$(".salary_update_type").val(salary.salaryItemType);
							$(".salary_update_memo").val(salary.salaryMemo);
							$(".salary_update_add option[value='"+salary.salaryAddItem+"']").attr("selected","");
							
							$(".salary_update_show option[value='"+salary.show+"']").attr("selected","");
							$(".salary_update_order_div").hide();
							console.log(salary.show);
							if(salary.show== "是"){
								$(".salary_update_order_container").show();
								$(".salary_update_order_show_div").show();
								var order = "第"+salary.salaryOrder+"位";
								$(".salary_update_order_show").val(order);
							}else{
								$(".salary_update_order_container").hide();
							}
							$(".salary_update_show").on("change",function(){
								var show = $(".salary_update_show option:selected").val();
								if(show=="是"){
									$(".salary_update_order_container").show();
									$(".salary_update_order_show_div").show();
									var order = "第"+salary.salaryOrder+"位";
									$(".salary_updat_order_show").val(order);
								}else if(show=="否"){
									$(".salary_update_order_container").hide();
								}
							});
							
							$(".salary_update_order").html("");
							$.ajax({
									type : "POST",
									url : "salaryItem/getOrder",
									success : function(data) {
										last_order = data.response;
										for(var i=1;i<=data.response;i++){
											$(".salary_update_order").append("<option value='"+i+"'>第"+i+"位</option>");
										}
										$(".salary_update_order option[value='"+salary.salaryOrder+"']").attr("selected","");
									}
							});
//							if(salary_item_update_btn == 1){
								$(".salary_update_order_show_btn").on("click",function(){
									$(".salary_update_order_show_div").hide();
									$(".salary_update_order_div").show();
									$(".salary_update_order_show").val("");
								});
								$(".salary_update_order_btn").on("click",function(){
									$(".salary_update_order_div").hide();
									$(".salary_update_order_show_div").show();
									$(".salary_update_order_show").val($(".salary_update_order option:selected").text());
								});
//							}
							if(salary.salaryItemType == "计算"){
								$(".salary_update_cal_container").show();
								$(".salary_update_cal_show_div").show();
								$(".salary_update_cal_show").val(salary.salaryCal);
								
								$(".update_cal_item_par1").html("");
								$.ajax({
									type : "POST",
									url : "salaryItem/getCalItems",
									success : function(data) {
										$.each(data.response,function(i){
											var item = data.response[i];
											$(".update_cal_item_par1").append("<option value='"+item.salaryItemId+"' text='"+item.salaryItemName+"'>"+item.salaryItemName+"</option>");
										});
									}
								});
								
								$(".salary_update_cal_upper_div").hide();
								$(".salary_update_cal_show_btn").on("click",function(){
									$(".salary_update_cal_show_div").hide();
									$(".salary_update_cal_upper_div").show();
									$(".salary_update_cal_show").val("");
									
									$(".update_cal_item_par1 option[text='"+salary.cal1+"']").attr("selected","");
									$(".update_cal_item_op option[value='"+salary.calOp+"']").attr("selected","");
									$(".update_cal_item_par2").val(salary.cal2);
								});
								$(".salary_update_cal_btn").on("click",function(){
									$(".salary_update_cal_upper_div").hide();
									$(".salary_update_cal_show_div").show();
									var cal = $(".update_cal_item_par1 option:selected").text()+$(".update_cal_item_op option:selected").text()+$(".update_cal_item_par2").val();
									$(".salary_update_cal_show").val(cal);
								});
							}
							else
								$(".salary_update_cal_container").hide();
					}
				});
				//end of load item info.
				if(open_update_modal == 1){
					$(".item_update_submit_btn").on("click",function(){
						var itemName = $(".salary_update_name").val();
						var itemType = $(".salary_update_type").val();
						var itemAdd = $(".salary_update_add option:selected").val();
						var itemMemo = $(".salary_update_memo").val();
						var itemShow = $(".salary_update_show option:selected").val();
						var itemOrder = $(".salary_update_order_show").val();
						var itemCal = $(".salary_update_cal_show").val();
						$.ajax({
							type : "POST",
							url : "salaryItem/update",
							data:{
								"itemId":item_id,
								"itemName":itemName,
								"itemType":itemType,
								"itemAdd":itemAdd,
								"itemMemo":itemMemo,
								"itemShow":itemShow,
								"itemOrder":itemOrder,
								"itemCal":itemCal,
							},
							success : function(data) {
								if(data.statusCode == 200)
									serarch_items((page_num-1)*10,10);
								$(".alert_modal").modal('toggle');
								$(".alert_info").text(data.message);
							}
						});
					})
				}
					
			});
			//end of update item event.	
				
			//handle update cal event
			var open_update_cal_modal = 0;
			$(".salary_item_update_cal_btn").on("click",function() {
				open_update_cal_modal++;
				$('.salary_update_cal_modal').modal('toggle');
				var item_id = $(this).attr("item_id");
				$.ajax({
					type : "POST",
					url : "salaryItem/loadItem",
					data:{
						"itemId":item_id,
					},
					success : function(data) {
						if(data.error){
							$(".alert_modal").modal('toggle');
							$(".alert_info").text(data.message);
							return;
						}
						var salary = data.response;
						$(".item_salary_update_cal_show").val(salary.salaryCal);
						$(".item_salary_update_cal_upper_div").hide();
						$(".item_update_cal_item_par1").html("");
						$.ajax({
							type : "POST",
							url : "salaryItem/getCalItems",
							success : function(data) {
								console.log(data);
								$.each(data.response,function(i){
									var item = data.response[i];
									$(".item_update_cal_item_par1").append("<option value='"+item.salaryItemId+"' text='"+item.salaryItemName+"'>"+item.salaryItemName+"</option>");
								});
							}
						});
						
						$(".item_salary_update_cal_show_div").show();
						$(".item_salary_update_cal_show_btn").on("click",function(){
							$(".item_salary_update_cal_show_div").hide();
							$(".item_salary_update_cal_upper_div").show();
							$(".item_salary_update_cal_show").val("");
							if(salary.salaryItemType == "计算"){
								$(".item_update_cal_item_par1 option[text='"+salary.cal1+"']").attr("selected","");
								$(".item_update_cal_item_op option[value='"+salary.calOp+"']").attr("selected","");
								$(".item_update_cal_item_par2").val(salary.cal2);
							}
						});
						$(".item_update_cal_item_op").on("change",function(){
							$(".item_update_cal_item_op").each(function(){
								$(this).removeAttr("selected");
							});
						})
						$(".item_salary_update_cal_btn").on("click",function(){
							$(".item_salary_update_cal_upper_div").hide();
							$(".item_salary_update_cal_show_div").show();
							var cal = $(".item_update_cal_item_par1 option:selected").text()+$(".item_update_cal_item_op option:selected").text()+$(".item_update_cal_item_par2").val();
							$(".item_salary_update_cal_show").val(cal);
						});
					}
				});
				if(open_update_cal_modal == 1){
					$(".item_update_cal_submit_btn").on("click",function(){
						var itemCal = $(".item_salary_update_cal_show").val();
						console.log(itemCal);
						$.ajax({
							type : "POST",
							url : "salaryItem/updateCal",
							data:{
								"itemId":item_id,
								"itemCal":itemCal,
							},
							success : function(data) {
								if(data.statusCode == 200)
									serarch_items((page_num-1)*10,10);
								$(".alert_modal").modal('toggle');
								$(".alert_info").text(data.message);
							}
						});
					})
				}
			});
			//end of update cal event
		}
		//end of ajax_load_table
		
		//handle add item event
		var update_last_order = 0;
		var open_add_modal = 0;
		$(".salary_add_cal").hide();
		$(".salary_item_add_btn").on("click",function() {
			open_add_modal++;
			$('.salary_add_modal').modal('toggle');
			$(".salary_add_name").val("");
			$(".salary_add_order").html("");
			$(".salary_add_memo").val("");
			
			$(".cal_item_par1").html("");
			$.ajax({
				type : "POST",
				url : "salaryItem/getCalItems",
				success : function(data) {
					console.log("calItems : "+data.response);
					$.each(data.response,function(i){
						var item = data.response[i];
						$(".cal_item_par1").append("<option value='"+item.salaryItemId+"'>"+item.salaryItemName+"</option>");
					});
				}
			});
			
			$(".salary_add_type").on("change",function(){
				var item_type = $(".salary_add_type option:selected").val();
				if(item_type == "计算"){
					$(".salary_add_cal").show();
				}
				else
					$(".salary_add_cal").hide();
			});
			
			$(".salary_add_order").html("");
			$.ajax({
				type : "POST",
				url : "salaryItem/getOrder",
				success : function(data) {
					console.log("show order : "+data.response);
					update_last_order = data.response + 1;
					for(var i=0;i<=data.response;i++){
						var num = i+1;
						$(".salary_add_order").append("<option value='"+num+"'>第"+num+"位</option>");
					}
					var length = data.response+1;
					$(".salary_add_order option:nth-child("+length+")").attr("selected","selected");
				}
			});
			
			$(".salary_add_show").on("change",function(){
				var item_type = $(".salary_add_show option:selected").val();
				if(item_type == "是"){
					$(".salary_add_order_div").show();
				}
				else{
					$(".salary_add_order_div").hide();
				}
			});

			//submit add
			if(open_add_modal == 1){
				$(".item_add_submit_btn").on("click",function(){
					var itemName = $(".salary_add_name").val();
					var itemType = $(".salary_add_type option:selected").val();
					var itemOrder = $(".salary_add_order option:selected").val();
					var itemAdd = $(".salary_add_add option:selected").val();
					var itemShow = $(".salary_add_show option:selected").val();
					var itemMemo = $(".salary_add_memo").val();
					var itemCalItemId = $(".cal_item_par1 option:selected").val();
					var itemCalPar1 = $(".cal_item_par1 option:selected").text();
					var itemOp = $(".cal_item_op option:selected").val();
					var itemCalPar2 = $(".cal_item_par2").val();
					$.ajax({
						type : "POST",
						url : "salaryItem/add",
						data:{
							"itemName":itemName,
							"itemType":itemType,
							"lastOrder":update_last_order,
							"itemOrder":itemOrder,
							"itemAdd":itemAdd,
							"itemShow":itemShow,
							"itemMemo":itemMemo,
							"itemCalItemId":itemCalItemId,
							"itemCalPar1":itemCalPar1,
							"itemOp":itemOp,
							"itemCalPar2":itemCalPar2,
						},
						success : function(data) {
							console.log(data);
							if(data.statusCode == 200)
								serarch_items((page_num-1)*10,10);
							$(".alert_modal").modal('toggle');
							$(".alert_info").text(data.message);
						}
					});
				})
			}
		});
})